package com.bestcem.xm.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.common.core.uitls.EnumUtil;
import com.bestcem.xm.member.dao.MemberDao;
import com.bestcem.xm.member.entity.pojo.MemberDO;
import com.bestcem.xm.member.enums.MemberTypeEnum;
import com.bestcem.xm.member.mq.message.MemberDeletedMessage;
import com.bestcem.xm.member.mq.message.MemberMessage;
import com.bestcem.xm.member.service.MemberMessageService;
import com.bestcem.xm.member.util.MemberUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.bestcem.xm.member.constant.Constants.EMPLOYEE_STATUS_MAP;

/**
 * 用于处理联系人消息
 *
 * @author yf.wang <yf.wang@idiaoyan.com>
 * @version V1.0
 * @date 2022/2/18
 */

@Slf4j
@Service
public class MemberMessageServiceImpl implements MemberMessageService {


    @Resource
    private MemberDao memberDao;

    @Resource
    private MemberUtil memberUtil;

    @Override
    public void handleMemberDelete(MemberDeletedMessage memberDeletedMessage) {
        List<String> ids = memberDeletedMessage.getIds();
        if (CollectionUtil.isNotEmpty(ids)) {
            // 获取联系人数据
            List<MemberDO> members = memberDao.findByIds(ids);
            // 机构联系人删除对应的项目
//            if(MemberTypeEnum.INSTITUTION.getIndex() == param.getMemberType()){
//                projectDao.deleteByMemberIds(param.getOrgId(), memberIds);
//            }
            if (CollectionUtil.isNotEmpty(members)) {
                for (MemberDO member : members) {
                    // 推送更新的联系人
                    MemberMessage message = new MemberMessage();
                    BeanUtil.copyProperties(member, message);
                    message.setOrgCode(memberDeletedMessage.getOrgCode());
                    message.setOrgName(memberDeletedMessage.getOrgName());
                    String identityType;
                    switch (EnumUtil.getEnumByIndex(MemberTypeEnum.class, member.getMemberType())) {
                        case INSTITUTION:
                            identityType = "企业对接人";
                            message.setNumberId("000");
                            break;
                        case INTERIOR:
                            identityType = ObjectUtil.isNotEmpty(member.getEmployeeStatus()) ? EMPLOYEE_STATUS_MAP.get(member.getEmployeeStatus()) : "";
                            break;
                        case INDIVIDUAL:
                            identityType = memberUtil.getIdentityType(member.getPhone(), memberDeletedMessage.getOrgId(), member.getEmail());
                            break;
                        default:
                            log.error("联系人类型异常：{}", member);
                            continue;
                    }
                    message.setIdentityType(identityType);
                    memberUtil.publishUpdateMember(message);
                }
            }
        }
        ServiceResult.success();
    }
}
